// pages/loginSys.js
var app = getApp();
var Zan = require('../../css/index')

Page(
  Object.assign({}, Zan.TopTips, {

    /**
     * 页面的初始数据
     */
    data: {
      hasCardPaw: false,     
      src: '',      
      cardPaw:'',
      vcode: '',
      showLogin:true, // when false = the user is reportLost page      
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {   
      this.showZanTopTips('若无验证码请在 个人中心-> 反馈 中提交反馈',4000); 
      if (options.showLogin == 'lost') {
        this.setData({ showLogin: false });
      }
      else{
        this.setData({ showLogin: true});
      }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      this.setData({ src: app.globalData.baseUrl + '/getPic?' + Math.random() * 1000 })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },


    submit: function (e) {
      var that = this;
      // this.data.zanTopTips.options.duration = 3000;
      let value = e.detail.value;
      let vcode = value.vcode;
      let cardPaw = value.cardPaw;
      if (vcode.length != 4) {
        this.showZanTopTips('请填写完整验证码');
        // this.setData({ src: '' })      
        return;
      }
      if (cardPaw == '') {
        this.showZanTopTips('请输入一卡通密码');
        return;
      }
      let sendData = {
        vcode: vcode,
        cardPaw: cardPaw,
        openid: app.globalData.openid,      
      }
      var flag=false;
      var tips='';
      if(that.data.showLogin==true){
        tips = '登录成功';
        wx.request({
          url: app.globalData.baseUrl + '/loginSys',
          data: sendData,
          method: 'POST',
          success:function(res){            
            if(res.data['isSuccess']){
              flag=true
            }
            if (flag) {
              wx.showToast({
                title: tips,
                icon: 'success',
                success: function () {
                  app.globalData.isLoginSys = true;
                }
              });
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 1600)
            }
            else {
              that.showZanTopTips('验证码或者密码错误！请重新输入');
              that.setData({ vcode: '', src: app.globalData.baseUrl + '/getPic?' + Math.random() * 1000, cardPaw: '' });
            }
        }
        })
      }
      else{
        tips='挂失成功';        
        wx.request({
          url: app.globalData.baseUrl + '/reportLost',
          data:sendData,
          method:'POST',
          success:function(res){            
            if (res.data['isSuccess']) {
              flag = true
            } 
            if (flag) {
              wx.showToast({
                title: tips,
                icon: 'success',
                success: function () {
                  app.globalData.isLoginSys = true;
                }
              });
              setTimeout(function () {
                wx.navigateBack({
                  delta: 1
                })
              }, 1600)
            }
            else {
              that.showZanTopTips('验证码或者密码错误！请重新输入');
              that.setData({ vcode: '', src: app.globalData.baseUrl + '/getPic?' + Math.random() * 1000, cardPaw: '' });
            }          
          }
        })        
      }      
      
      
    }
  }))